package com.xmh.factorydemo.factory;

import com.xmh.factorydemo.service.UserService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@PropertySource(value = "factory.properties")
public class StrategyFactory implements ApplicationContextAware {

    @Value("${FactoryUser.ImplClassName}")
    private String userServiceImplType;

    private static UserService userService;

    public static UserService getUserService() {
        return userService;
    }

    private void setUserService(UserService userService) {
        StrategyFactory.userService = userService;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        setUserService((UserService) applicationContext.getBean(userServiceImplType));

//		applicationContext.getBeansOfType(UserService.class).values().forEach(e -> {
//			if (e.getClass().getName().equals(userServiceImplType))
//				userService = e;
//		});
    }

}
